package test

import (
	"go_learn/actor"
	"go_learn/app"
	"go_learn/example/shape"
	"go_learn/mlog"
	"math"
)

type Test struct {
	actor.ConcurrentActor
	shapeAddr uint
	sphereR   float64
}

func (slf *Test) Name() string {
	return "test"
}

func (slf *Test) OnInit() {
	slf.RegistHandle(actor.Normal, "test1", slf.test1)
	slf.RegistHandle(actor.Normal, "test2", slf.test2)
	slf.RegistHandle(actor.Normal, "test3", slf.test3)
	slf.RegistHandle(actor.Normal, "getSphereInfo", slf.getSphereInfo)
}

func (slf *Test) Run(args interface{}) {
	s := shape.Shape{}
	slf.shapeAddr, _ = app.App.NewActor(&s.ConcurrentActor, &s)
	var x, y float64 = 100, 100
	rst, err := slf.SyncCall(slf.shapeAddr, actor.Normal, "rectangle", x, y)
	mlog.Debugf("Init Call Rst ", rst, err)
}

func (slf *Test) OnDestroy() {

}

func (slf *Test) test1(source uint) (ret interface{}, err error) {
	slf.Send(slf.shapeAddr, actor.Normal, "triangle", 10.0, 2.0)
	var r float64 = 10
	rst, err := slf.Call(slf.shapeAddr, actor.Normal, "circle", r)
	mlog.Debugf("Test1 Call Circle Rst ", rst, err)
	slf.sphereR = rst.([2]interface{})[0].(float64)
	rst, err = slf.Call(slf.shapeAddr, actor.Normal, "sphere")
	mlog.Debugf("Test1 Call Sphere Rst ", rst, err)
	return
}

func (slf *Test) getSphereInfo(source uint) (ret interface{}, err error) {
	ret = [...]interface{}{4 / 3 * math.Pi, slf.sphereR}
	return
}

func (slf *Test) test2(source uint) (ret interface{}, err error) {
	return
}

func (slf *Test) test3(source uint) (ret interface{}, err error) {
	return
}
